<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$return=array();
$tabs=array(
	array('op'=>'edit','title'=>'修改价格'),
	array('op'=>'log','title'=>'操作日志'),
);
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	else{
		$item=array();
		if($_RQ['parentid']){
			$item['parentid']=intval($_RQ['parentid']);
			$item['parenttitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['parentid']),'title');
		}
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>$tabs,
		'edit'=>array(
				array('name'=>'pcate','title'=>'商品分类','type'=>'select','options'=>'groups'),	
				array('name'=>'type','title'=>'修改方向','type'=>'radio','options'=>'types'),
				array('name'=>'fee','title'=>'变动幅度','type'=>'string'),
				
		)
	);
	$return['groups']=pdo_getall('jy_weishop_goods_group',array('parentid'=>$id,'onepluginname'=>'jy_textfee','uniacid'=>$_SESSION['uniacid']),'','id');
	$return['types']=array('增加','减少','乘','除');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['pcate']){
		exi('商品分类不能为空','error');
	}
	if(!$postdata['fee']){
		exi('变动幅度不能为空','error');
	}
	if($postdata['type']==1){
		$data=array(
			'price -='=>$postdata['fee']
		);
	}elseif($postdata['type']==2){
		$data=array(
			'price *='=>$postdata['fee']
		);
	}elseif($postdata['type']==3){
		$data=array(
			'price /='=>$postdata['fee']
		);
	}else{
		$data=array(
			'price +='=>$postdata['fee']
		);
	}
	pdo_update('jy_weishop_goods',$data,array('pcate'=>$postdata['pcate'],'uniacid'=>$_SESSION['uniacid']));
	$goodsids=pdo_getall('jy_weishop_goods',array('pcate'=>$postdata['pcate'],'uniacid'=>$_SESSION['uniacid']),'id','id');
	if($goodsids){
		$goodsids=array_keys($goodsids);
		pdo_update('jy_weishop_goods_option',$data,array('goodsid'=>$goodsids,'uniacid'=>$_SESSION['uniacid']));
	}
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'pcate'=>$postdata['pcate'],
			'type'=>$postdata['type'],
			'fee'=>$postdata['fee'],
			'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_weishop_upprice_log',$insert);
	exi('操作成功！');
}
if($_RQ['op']=='log'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['type']) && $_RQ['type']!=100){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=intval($_RQ['type']);
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:20;
	$list=pdo_getslice('jy_weishop_upprice_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['grouptitle']=pdo_getcolumn('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pcate']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'grouptitle','title'=>'商品分类','type'=>'string'),
			array('name'=>'type','title'=>'操作类型','type'=>'radio','options'=>'types'),
			array('name'=>'fee','title'=>'变动幅度','type'=>'string'),
			array('name'=>'createtime','title'=>'预约时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'type','options'=>'types','title'=>'类型'),
		),
	);
	$return['types']=array('增加','减少','乘','除');
	$return['htmls']=$htmls;	
	exi($return);
}